home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / MAP Viewer / TextureManager.cpp < prev    next >
C/C++ Source or Header  |  2003-10-09  |  7KB  |  230 lines

  1. /*
  2. Half-Life MAP viewing utility.
  3. Copyright (C) 2003  Ryan Samuel Gregg
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include "stdafx.h"
  21. #include "TextureManager.h"
  22.  
  23. CTextureManager::CTextureManager(CConfig *Config, CRichTextBox *txtConsole)
  24. {
  25.     this->Config = Config;
  26.     this->txtConsole = txtConsole;
  27.  
  28.     Textures = new ArrayList();
  29. }
  30.  
  31. int CTextureManager::AddTexture(String *TextureName)
  32. {
  33.     TextureName = TextureName->ToLower();
  34.  
  35.     for(int i = 0; i < Textures->Count; i++)
  36.     {
  37.         if(static_cast<Texture*>(Textures->get_Item(i))->TextureName->Equals(TextureName))
  38.         {
  39.             return i;
  40.         }
  41.     }
  42.  
  43.     Textures->Add(new Texture(TextureName));
  44.     return Textures->Count - 1;
  45. }
  46.  
  47. ArrayList *CTextureManager::GetTextures()
  48. {
  49.     return Textures;
  50. }
  51.  
  52. Texture *CTextureManager::GetTexture(int iIndex)
  53. {
  54.     return static_cast<Texture*>(Textures->get_Item(iIndex));
  55. }
  56.  
  57. unsigned int CTextureManager::GetID(int iIndex)
  58. {
  59.     return static_cast<Texture*>(Textures->get_Item(iIndex))->ID;
  60. }
  61.  
  62. unsigned long CTextureManager::GetWidth(int iIndex)
  63. {
  64.     return static_cast<Texture*>(Textures->get_Item(iIndex))->Width;
  65. }
  66.  
  67. unsigned long CTextureManager::GetHeight(int iIndex)
  68. {
  69.     return static_cast<Texture*>(Textures->get_Item(iIndex))->Height;
  70. }
  71.  
  72. String *CTextureManager::GetName(int iIndex)
  73. {
  74.     return static_cast<Texture*>(Textures->get_Item(iIndex))->TextureName;
  75. }
  76.  
  77. void CTextureManager::LoadTextures(String *WADString)
  78. {
  79.     int iTexturesLoaded = 0, iTotalTexturesLoaded = 0;
  80.     WADString = WADString->Replace("../", "");
  81.     WADString = WADString->Replace('/', '\\');
  82.     String *WAD, *WADs[] = WADString->Split(S";"->ToCharArray());
  83.  
  84.     bool bWADFound;
  85.     LPCTSTR    szDrive= NULL;
  86.     String *Drives[] = System::IO::Directory::GetLogicalDrives();
  87.  
  88.     for(int i = 0; i < Drives->Length; i++)
  89.     {
  90.         szDrive = (LPCTSTR)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(Drives[i]).ToPointer());
  91.  
  92.         if(GetDriveType(szDrive) != DRIVE_FIXED)
  93.             Drives[i] = "";
  94.  
  95.         System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr((void*)szDrive)));
  96.     }
  97.  
  98.     for(int i = 0; i < WADs->Count; i++)
  99.     {
  100.         bWADFound = false;
  101.  
  102.         //CWADLoader::LoadWADFile(String::Concat(Config->sHalfLifePath, WADs[i]), Textures);
  103.         WAD = String::Concat(Config->sHalfLifePath, WADs[i])->Replace("\\\\", "\\");
  104.  
  105.         bWADFound = System::IO::File::Exists(WAD);
  106.  
  107.         if(!bWADFound)
  108.         {
  109.             for(int j = 0; j < Drives->Length; j++)
  110.             {
  111.                 if(Drives[j]->Length == 0)
  112.                     continue;
  113.  
  114.                 WAD = String::Concat(Drives[j], WADs[i])->Replace("\\\\", "\\");
  115.                 bWADFound = System::IO::File::Exists(WAD);
  116.  
  117.                 if(bWADFound)
  118.                     break;
  119.             }
  120.         }
  121.  
  122.         if(bWADFound)
  123.         {
  124.             txtConsole->Print(String::Concat(S"Loading ", WAD, S"..."), Color::Gray);
  125.             CWADLoader::LoadWADFile(WAD, Textures, &iTexturesLoaded);
  126.             txtConsole->Print(String::Concat(iTexturesLoaded.ToString(), S" textures loaded.", S"\n"), Color::Gray);
  127.  
  128.             iTotalTexturesLoaded += iTexturesLoaded;
  129.         }
  130.         else
  131.         {
  132.             txtConsole->Print(String::Concat(WADs[i], S" not found.", S"\n"), Color::Red);
  133.         }
  134.     }
  135.     
  136.     txtConsole->Print(String::Concat(iTotalTexturesLoaded.ToString(), S" out of ", Textures->Count.ToString(), S" textures loaded.", S"\n"), Color::Green);
  137.  
  138.     UpdateTextureFilter();
  139. }
  140.  
  141. void CTextureManager::UpdateTextureFilter()
  142. {
  143.     Texture *Tex;
  144.     int iMaxAnisotropy = 0;
  145.  
  146.     if(Config->eTextureFilter == TextureFilter::MipAnisotropic)
  147.     {
  148.         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &iMaxAnisotropy);
  149.     }
  150.  
  151.     glEnable(GL_TEXTURE_2D);
  152.  
  153.     for(int i = 0; i < Textures->Count; i++)
  154.     {
  155.         Tex = static_cast<Texture*>(Textures->get_Item(i));
  156.         if(Tex->Loaded)
  157.         {
  158.             glBindTexture(GL_TEXTURE_2D, Tex->ID);
  159.             switch(Config->eTextureFilter)
  160.             {
  161.                 case TextureFilter::Nearest:
  162.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  163.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  164.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  165.                     break;
  166.                 case TextureFilter::Linear:
  167.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  168.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  169.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  170.                     break;
  171.                 case TextureFilter::MipNearest:
  172.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  173.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  174.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  175.                     break;
  176.                 case TextureFilter::MipLinear:
  177.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  178.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  179.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  180.                     break;
  181.                 case TextureFilter::MipBilinear:
  182.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  183.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  184.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  185.                     break;
  186.                 case TextureFilter::MipTrilinear:
  187.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0);
  188.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  189.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  190.                     break;
  191.                 case TextureFilter::MipIsotropic:
  192.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  193.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  194.                     break;
  195.                 case TextureFilter::MipAnisotropic:
  196.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  197.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, iMaxAnisotropy);
  198.                     break;
  199.                 /*default:
  200.                     int iMaxAnisotropy;
  201.                     glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &iMaxAnisotropy);
  202.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  203.                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, iMaxAnisotropy);
  204.                     break;*/
  205.             }
  206.         }
  207.     }
  208. }
  209.  
  210. void CTextureManager::DestroyTextures()
  211. {
  212.     Texture *Tex;
  213.     unsigned int ID;
  214.     int iCount = 0;
  215.     for(int i = 0; i < Textures->Count; i++)
  216.     {
  217.         Tex = static_cast<Texture*>(Textures->get_Item(i));
  218.         if(Tex->Loaded)
  219.         {
  220.             ID = Tex->ID;
  221.             glDeleteTextures(1, &ID);
  222.             Tex->ID = ID;
  223.             iCount++;
  224.         }
  225.     }
  226.  
  227.     txtConsole->Print(String::Concat(iCount.ToString(), S" textures destroyed.", S"\n"), Color::Green);
  228.  
  229.     Textures->Clear();
  230. }